#encoding=utf8
'''
Created on Apr 14, 2015

@author: jingyang <jingyang@smalltreemedia.com>
'''
from django.contrib import admin
from django.contrib.admin.util import unquote
from django.db import transaction
from django.utils.decorators import method_decorator
from django.views.decorators.csrf import csrf_protect

from notification.models import Notice, ReportReceiver
from utils import field, DateListFilter

csrf_protect_m = method_decorator(csrf_protect)


class YearListFilter(DateListFilter):
    title = 'Received at Year'
    parameter_name = 'year'
    filter_field = 'create_time'


class MonthListFilter(DateListFilter):
    title = 'Received at Month'
    parameter_name = 'month'
    filter_field = 'create_time'


class DayListFilter(DateListFilter):
    title = 'Received at Day'
    parameter_name = 'day'
    filter_field = 'create_time'


class NoticeAdmin(admin.ModelAdmin):
    list_per_page = 20
    list_display = ('get_subject', 'read', 'get_sender', 'sent_time')
    list_filter = ('read', YearListFilter, MonthListFilter, DayListFilter)
    actions = ['mark_as_read']
    search_fields = ('sender__username', 'content')
    readonly_fields = ('sender', 'get_sender', 'subject', 'get_subject', 'content', 'get_content',
        'sent_time')
    change_form_template = 'notice_changeform.html'
    fieldsets = (
        (None, {
            'fields':
            (
            'get_sender',
            'get_subject',
            'get_content',
            'sent_time',
            )
            }),
    )

    @field('Mark notification(s) as read')
    def mark_as_read(self, request, queryset):
        count = queryset.update(read=True, showed=True)
        if count == 1:
            msg = '1 notification was'
        else:
            msg = '%s notifications were' % count
        self.message_user(request, '%s successfully marked as read.' % msg)

    @field('Subject', allow_tags=True, admin_order_field='subject')
    def get_subject(self, obj):
        return obj.subject

    @field('Sender', admin_order_field='sender')
    def get_sender(self, obj):
        return obj.sender or 'system'

    @field('Content', allow_tags=True)
    def get_content(self, obj):
        return obj.content

    def get_actions(self, request):
        """
        remove delete action
        """
        actions = admin.ModelAdmin.get_actions(self, request)
        if actions.get('delete_selected'):
            del actions['delete_selected']
        return actions

    def queryset(self, request):
        qs = admin.ModelAdmin.queryset(self, request)
        qs = qs.filter(receiver=request.user)
        return qs

    def has_add_permission(self, request):
        return False

    @csrf_protect_m
    @transaction.commit_on_success
    def change_view(self, request, object_id, form_url='', extra_context=None):
        obj = self.get_object(request, unquote(object_id))
        if obj and not obj.read:
            obj.read = True
            obj.showed = True
            obj.save()
        extra_context = {'title': 'Notification Detail'}
        return admin.ModelAdmin.change_view(self, request, object_id, form_url=form_url, extra_context=extra_context)


class ReportReceiverAdmin(admin.ModelAdmin):
    list_display = ('name', 'email', 'is_active')
    list_filter = ('is_active',)
    search_fields = ('name', 'email')


admin.site.register(Notice, NoticeAdmin)
admin.site.register(ReportReceiver, ReportReceiverAdmin)
